﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Actuals.CoreDomain.Business;
using Actuals.CoreDomain.Data;
using Actuals.CoreDomain;
using Actuals.Data;
using Actuals.Common;


namespace Actuals.Business
{
    public class BusinessTechnicalTask : IBusinessTechnicalTask
    {
        private IDalTechnicalTask _dalTechnicalTask;

        public BusinessTechnicalTask(IDalTechnicalTask dalTechnicalTask)
        {
            _dalTechnicalTask = dalTechnicalTask;
        }
        public BusinessTechnicalTask()
        {
            _dalTechnicalTask = new DalTechnicalTask();
        }
        public int AddTechnicalTask(ITechnicalTask technicalTask)
        {
            technicalTask.RemainingTime = technicalTask.EstimatedTime;
            technicalTask.TotalElapsedTime = 0;
            technicalTask.TimeSpent = 0;
            //bool retValue = false;

            //_dalTechnicalTask.AddTechnicalTask(technicalTask);




            return _dalTechnicalTask.AddTechnicalTask(technicalTask);
        }
        public int UpdateTechnicalTask(ITechnicalTask technicalTask, int employeeId)
        {
            if (technicalTask.TimeSpent > 0)
            {
                IBusinessUserStory businessUserStory = new BusinessUserStory();
                IUserStory userStory = businessUserStory.GetUserStory(technicalTask.UserStoryId);

                IBusinessTask businessTask = new BusinessTask();
                ITask task = businessTask.GetTask(userStory.TaskId);


                IActual actual = new Actual();
                actual.EmployeeId = employeeId;
                actual.Description = technicalTask.UserStoryDescription;
                actual.date = DateTime.Now;
                actual.NoOfHours = technicalTask.TimeSpent;
                actual.TaskId = task.TaskId;
                actual.ProjectId = task.ProjectId;
                actual.ReleaseId = task.ReleaseId;

                IBusinessActual businessActual = new BusinessActual();
                businessActual.AddActual(actual);
                

            }
            technicalTask.TotalElapsedTime = technicalTask.TotalElapsedTime + technicalTask.TimeSpent;
            technicalTask.RemainingTime = technicalTask.EstimatedTime - technicalTask.TotalElapsedTime;

            return UpdateTechnicalTask(technicalTask);


            //actual.




        }


        public ITechnicalTask GetTechnicalTask(int technicalTaskId)
        {
            return _dalTechnicalTask.GetTechnicalTask(technicalTaskId);

        }
        public IList<ITechnicalTask> GetTechnicalTasks(int userStoryId)
        {
            return _dalTechnicalTask.GetTechnicalTasks(userStoryId);
        }

        /*  public bool DeleteTechnicalTask(ITechnicalTask technicalTask)
          {
              return _dalTechnicalTask.DeleteTechnicalTask(technicalTask);
          }*/
        public int DeleteTechnicalTask(int technicalTaskId)
        {
            return _dalTechnicalTask.DeleteTechnicalTask(technicalTaskId);
        }
        public ITechnicalTask GetTechTaskWithUserStoryId(int userStoryId)
        {
            return _dalTechnicalTask.GetTechTaskWithUserStoryId(userStoryId);
        }
        public int UpdateTechnicalTask(ITechnicalTask technicalTask)
        {            
            return _dalTechnicalTask.UpdateTechnicalTask(technicalTask);
        }
        public IList<ITechnicalTask> getTechTaskWithSprintId(int sprintId)
        {
            //return new List<ITechnicalTask>();
           return  _dalTechnicalTask.getTechTaskWithSprintId(sprintId);
        }
        public int UpdateIncompleteTechTasks(ITechnicalTask inCompleteTechnicalTask)
        {
            return _dalTechnicalTask.UpdateIncompleteTechTasks(inCompleteTechnicalTask);
        }

        public int GetInCompleteTaskCountForSprint(int sprintId)
        {

            IList<ITechnicalTask> technicalTasks = getTechTaskWithSprintId(sprintId);

            int count = 0;
            foreach (ITechnicalTask technicaltask in technicalTasks)
            {
                string _status = technicaltask.Status;
                if ((_status == Constants.TechnicalTaskStatus.InProgress)
                    || (_status == Constants.TechnicalTaskStatus.ToDo))
                {
                    count++;
                }
            }
            return count;
        }

        public IList<ITechnicalTask> GetInDcompleteTechnicalTaskInCurrentSprint(int sprintId)
        {
            IList<ITechnicalTask> technicalTasks = getTechTaskWithSprintId(sprintId); //Getting whole technicalTasks using sprintId
            IList <ITechnicalTask>  inCompleteTechnicalTasks = new List<ITechnicalTask>();
            foreach (ITechnicalTask incompleteTechnicalTask in technicalTasks)  //Getting only the incompleted TechnicalTasks
            {
                string _status = incompleteTechnicalTask.Status;
                if ((_status == Constants.TechnicalTaskStatus.InProgress)
                    || (_status == Constants.TechnicalTaskStatus.ToDo))
                {
                    inCompleteTechnicalTasks.Add(incompleteTechnicalTask);
                }
            }
            return inCompleteTechnicalTasks;

        }

        public int UpdateTechTaskStatus(ITechnicalTask technicalTask)
        {
            return _dalTechnicalTask.UpdateTechTaskStatus(technicalTask);
        }
        public IList<IUserStory> GetDistinctUserStory(int sprintId)
        {
             IList<ITechnicalTask> technicalTasks = getTechTaskWithSprintId(sprintId);
            List<int> distinctUserStories = technicalTasks.Select(x => x.UserStoryId).Distinct().ToList();//Getting distinct userstories in List of technicalTasks
            IList<IUserStory> userStories = new List<IUserStory>();
            foreach (int distinctUserStory in distinctUserStories)
            {

                IBusinessUserStory businessUserStory = new BusinessUserStory();
                IUserStory userStory = businessUserStory.GetUserStory(distinctUserStory); //Getting list of IUserStory for distinct UserStoryId
                userStories.Add(userStory);
            }
            return userStories;

        }
      /* public IList<int> GetDistinctUserStory(int sprintId)    //Getting TechnicalTask row with distinct UserStoryId
        {
            IList<ITechnicalTask> inCompleteTechnicalTasks = GetInDcompleteTechnicalTaskInCurrentSprint(sprintId);
            List<int> distinctUserStories = inCompleteTechnicalTasks.Select(x => x.UserStoryId).Distinct().ToList();
            return distinctUserStories;
        }*/
    }
}
